#include "widget.h"
#include "ui_widget.h"
#include<QRegExpValidator>
#include<QDebug>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ui->lineEdit_name->setPlaceholderText("请输入你的姓名");
    ui->lineEdit_name->setClearButtonEnabled(true);

    ui->lineEdit_password_1->setPlaceholderText("请输入你的密码");
    ui->lineEdit_password_1->setClearButtonEnabled(true);
    ui->lineEdit_password_1->setEchoMode(QLineEdit::Password);
    ui->lineEdit_password_2->setPlaceholderText("请再次输入你的密码");
    ui->lineEdit_password_2->setClearButtonEnabled(true);
    ui->lineEdit_password_2->setEchoMode(QLineEdit::Password);

    ui->lineEdit_phone->setPlaceholderText("请输入你的电话号码");
    ui->lineEdit_phone->setClearButtonEnabled(true);
    //创建一个验证器对象
    ui->lineEdit_phone->setValidator(new QRegExpValidator(QRegExp("^1\\d{10}$")));

    ui->pushButton_confirm->setEnabled(false);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_checkBox_toggled(bool checked)
{
    if(checked)
    {
        ui->lineEdit_password_1->setEchoMode(QLineEdit::Normal);
        ui->lineEdit_password_2->setEchoMode(QLineEdit::Normal);
    }
    else
    {
        ui->lineEdit_password_1->setEchoMode(QLineEdit::Password);
        ui->lineEdit_password_2->setEchoMode(QLineEdit::Password);
    }
}

void Widget::on_lineEdit_phone_textEdited(const QString &arg1)
{
    QString text=arg1;
    int pos=0;
    if(ui->lineEdit_phone->validator()->validate(text,pos)==QValidator::Acceptable)
    {
        ui->pushButton_confirm->setEnabled(true);
    }
    else
    {
        ui->pushButton_confirm->setEnabled(false);
    }
}

void Widget::on_lineEdit_password_1_textChanged(const QString &arg1)
{
    (void) arg1;
    Checked();
}

void Widget::on_lineEdit_password_2_textChanged(const QString &arg1)
{
    (void) arg1;
    Checked();
}

void Widget::Checked()
{
    QString s1=ui->lineEdit_password_1->text();
    QString s2=ui->lineEdit_password_2->text();
    if(s1==s2) ui->label_good->setText("密码一致");
    else ui->label_good->setText("密码不一致");
}

void Widget::on_pushButton_confirm_clicked()
{
    QString sex = ui->radioButton_male->isChecked()?"男":"女";

    qDebug()<<"姓名："<<ui->lineEdit_name->text()
            <<" 密码："<<ui->lineEdit_password_1->text()
            <<" 电话："<<ui->lineEdit_phone->text()
            <<" 性别："<<sex;
}
